/**
 * 餐饮饭店信息JS
 * @author Assassin
 */
$(document).ready(function(){
    $('#uploadPictureLink').on('click',function(){
        $('#pictureFile').click();
    });
    $('#pictureImg').on('click',function(){
        $('#pictureFile').click();
    });
});

// 上传缩略图片
function uploadPicture(){
    $.ajaxFileUpload({
        url:shiro.baseUrl + "uploadPicture",
        secureuri:false,
        fileElementId:"pictureFile",
        dataType: 'json',
        success: function (data) {
            $("#pictureImg").attr("src", data.preUrl + "?v=" + new Date());
            $('#picture').val(data.picture);
        },error:function(XMLHttpRequest, textStatus, errorThrown){
            layer.msg(errorThrown, {icon: 5,time: 1000});
        }
    });
}

// 执行添加餐饮饭店信息
function executeAddRestaurant() {
    var restaurantName = $.trim($('#restaurantName').val());
    if(shiro.util.isEmpty(restaurantName)) {
        layer.tips('饭店名称不能为空', '#restaurantName');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('饭店地址不能为空', '#address');
        return false;
    }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('饭店描述不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    // 提交信息
    shiro.ajax({
        url: 'restaurant/executeAddRestaurant',
        data:{'restaurantName': restaurantName, "address":address,'content':content,"picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "restaurant/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
            }
        }
    });
    return false;
}

// 执行修改餐饮饭店信息
function executeEditRestaurant() {
	var restaurantName = $.trim($('#restaurantName').val());
    if(shiro.util.isEmpty(restaurantName)) {
        layer.tips('饭店名称不能为空', '#restaurantName');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('饭店地址不能为空', '#address');
        return false;
    }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('饭店描述不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    var restaurantId = $('#restaurantId').val();
    // 提交信息
    shiro.ajax({
        url: 'restaurant/executeEditRestaurant',
        data:{'restaurantId': restaurantId, 'restaurantName': restaurantName, "address":address,'content':content,"picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "restaurant/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
            }
        }
    });
    return false;
}

// 删除餐饮饭店
function deleteRestaurant(restaurantId,status) {
    layer.msg('确定删除该餐饮饭店？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'restaurant/delete',
                data:{"restaurantId": restaurantId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            if(status==2){
                            	window.location.href = shiro.baseUrl + "restaurant/list";
                            }else{
                            	window.location.href = shiro.baseUrl + "restaurant/listNo";
                            }
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除餐饮饭店
function batchDeleteRestaurant() {
    var restaurantIdArray = shiro.form.getCheckBoxSelectValues('restaurantIdCheckBox');
    if(restaurantIdArray.length == 0) {
        layer.msg('请选择要操作的餐饮饭店', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的餐饮饭店？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var restaurantIds = restaurantIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'restaurant/batchDelete',
                    data:{"restaurantIds": restaurantIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "restaurant/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}



//通过餐饮饭店
function adoptRestaurant(restaurantId) {
    layer.msg('确定审核通过该餐饮饭店？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'restaurant/adoptRestaurant',
                data:{"restaurantId": restaurantId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "restaurant/listNo";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

/**
 * 执行驳回
 */
function rejectRestaurant(restaurantId){
	layer.open({
		type: 2,
		area: ['400px', '230px'],
		title: '填写驳回原因',
		content: shiro.baseUrl + 'restaurant/rejectRestaurant?restaurantId='+restaurantId
	});
}

/**
 * 更新驳回原因
 */
function updateReason(){
	var restaurantId = $.trim($('#restaurantId').val());
	var reason = $.trim($('#reason').val());
	if(shiro.util.isEmpty(reason)) {
        layer.msg('驳回原因不能为空', {icon: 5,time: 1000});
        return false;
    }
	 // 提交信息
    shiro.ajax({
        url: 'restaurant/updateReason',
        data:{"restaurantId":restaurantId,"reason":reason},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                	parent.layer.closeAll();
                	parent.window.location.href = shiro.baseUrl + "restaurant/listNo";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
            }
        }
    });
    return false;
}

/**
 * 查看
 */
function viewRestaurant(restaurantId){
	layer.open({
		type: 2,
		area: ['400px', '230px'],
		title: '查看驳回原因',
		content: shiro.baseUrl + 'restaurant/viewRestaurant?restaurantId='+restaurantId
	});
}

